//
//  KeyboardViewController.swift
//  HHKeyboard
//
//  Created by Hay Huang on 2025/7/29.
//

import UIKit
import KeyboardKit
import SwiftUICore
import SwiftUI
import React


struct RNView: UIViewRepresentable {
  var controller: KeyboardViewController?
  func makeUIView(context: Context) -> UIView {

    let jsCodeLocation = Bundle.main.url(forResource: "main", withExtension: "jsbundle")
    let bridge = RCTBridge(bundleURL: jsCodeLocation, moduleProvider: nil, launchOptions: nil)
    let rootView = RCTRootView(bridge: bridge!, moduleName: "KeyboardView", initialProperties: nil)
    if let keyboardBridge = bridge?.module(for: KeyboardViewModule.self) as? KeyboardViewModule {
      keyboardBridge.keyboardViewController = controller
    }
        return rootView
    }

  func updateUIView(_ uiView: UIView, context: Context) {}
}


class KeyboardViewController: KeyboardInputViewController {
    
  override func viewWillSetupKeyboardView() {
  

        setupKeyboardView { [weak self] controller in // <-- Use weak or unknowned self!
            KeyboardView(
                state: controller.state,
                services: controller.services,
                buttonContent: { $0.view },
                buttonView: { $0.view },
                collapsedView: { $0.view },
                emojiKeyboard: { $0.view },
                toolbar: { _ in
                  Keyboard.Toolbar {
                      HStack {
                          Spacer()
                        RNView(controller: self)
                          Spacer()
                      }
                  }
                }
            )
        }
    }

}
